package net.noday.demo.service.impl;

import java.util.List;

import net.noday.demo.domain.User;
import net.noday.demo.persistent.jdbcImpl.JdbcUserDAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl {

	@Autowired
	private JdbcUserDAO userDAO;
	
	public boolean add(User user) {
		boolean status = false;
		try {
			user = userDAO.save(user);
			status = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return status;
	}
	
	public User loadById(int uid) {
		return userDAO.loadByUid(uid);
	}
	
	public void update(User user) {
		userDAO.update(user);
	}
	
	public void delete(int uid) {
		userDAO.delById(uid);
	}
	
	public List<User> getAll() {
		return userDAO.listAll();
	}
	
	/**
	 * 分页 升序
	 * @param pageFrom 当前页
	 * @param pageTo 目标页
	 * @param uid 最小或最大id
	 * @param pageSize 每页数量
	 * @return
	 */
	public List<User> getAll(int pageFrom, int pageTo, int uid, int pageSize) {
		int value = pageTo - pageFrom;
		boolean forword = value > 0;
		return userDAO.listAllByPage(new Object[]{forword ? ">" : "<", uid, 
				forword ? "asc" : "desc", (value - 1) * pageSize, pageSize});
	}
}
